package com.chinalife.project.controller;

import com.chinalife.project.entity.DailyInfo;
import com.chinalife.project.service.DailyInfoService;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.web.bind.annotation.*;

/**
 * 每日处罚信息(DailyInfo)表控制层
 *
 * @author makejava
 * @since 2024-07-18 15:06:57
 */
@RestController
@RequestMapping("dailyInfo")
public class DailyInfoController {

    private final ThreadPoolExecutor executor;
    private final DailyInfoService dailyInfoService;

    public DailyInfoController(DailyInfoService dailyInfoService,
                               ThreadPoolExecutor executor) {
        this.dailyInfoService = dailyInfoService;
        this.executor = executor;
    }

    /**
     * 分页查询
     *
     * @param dailyInfo   筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<DailyInfo>> queryByPage(DailyInfo dailyInfo, PageRequest pageRequest) {
        executor.execute(() -> dailyInfoService.queryByPage(dailyInfo, pageRequest));
        return ResponseEntity.ok(this.dailyInfoService.queryByPage(dailyInfo, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<DailyInfo> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.dailyInfoService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param dailyInfo 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<DailyInfo> add(DailyInfo dailyInfo) {
        return ResponseEntity.ok(this.dailyInfoService.insert(dailyInfo));
    }

    /**
     * 编辑数据
     *
     * @param dailyInfo 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<DailyInfo> edit(DailyInfo dailyInfo) {
        return ResponseEntity.ok(this.dailyInfoService.update(dailyInfo));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.dailyInfoService.deleteById(id));
    }

}

